<template>
  <div>
    <Home-header ></Home-header>
    <Home-swiper :list="swiperList"></Home-swiper>
    <Home-icons :list="iconsList"></Home-icons>
    <Home-recommend :list="recommendList"></Home-recommend>
    <Home-weekend :list="weekendList"></Home-weekend>
  </div>
</template>
<!-- ajax 官方推荐组件为axios  项目目录安装 cnpm install axios --save -->
<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
import axios from 'axios'
import { mapState } from 'vuex'

export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data () {
    return {
      lastCity: '',
      swiperList: [],
      iconsList: [],
      recommendList: [],
      weekendList: []
    }
  },
  computed: {
    ...mapState(['city'])
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json?city=' + this.city)
        .then(this.getHomeInfoRes)
    },
    getHomeInfoRes (res) {
      res = res.data
      if (res.ret && res.data) {
        const data = res.data
        this.swiperList = data.swiperList
        this.iconsList = data.iconsList
        this.recommendList = data.recommendList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    /** 将city存一下 */
    this.lastCity = this.city
    this.getHomeInfo()
  },
  /** 当使用缓存 <keep-alive>时 只会执行此函数（activated：当页面重新显示的时候执行） */
  activated () {
    /** 当当前跟之前的不想等则重新发起ajax请求 */
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style scoped>

</style>
